# serialcolor-lib.pl
# common functions for the livecdmaker module

do "../web-lib.pl";
&init_config();
do "../ui-lib.pl";


# item_br([number])
# form item <br>
sub item_br() {
	my $ReturnValue = "";
	
	if ($_[0] eq "") {
		$ReturnValue .= "<br>\n";
	} else {
		for (my $i=1; $i<=$_[0]; $i++) {
			$ReturnValue .= "<br>\n";
		}
	}
	
	return $ReturnValue;
}

# item_hr()
# form item separate horizon bar
sub item_hr() {
	my $ReturnValue = "";
	
	$ReturnValue .= "<br>\n<br>\n";
	$ReturnValue .= &ui_hr();
	
	return $ReturnValue;
}

# search_serialport(type, [sort])
# search serialport name or path
sub search_serialport() {
	$FH = "/proc/tty/drivers";
	$type = $_[0];
	$sort = $_[1];
	my @ReturnArray = ();
	
	if ( -e $FH ) {
		open(FH, $FH);
		
		while( $line = <FH> ) {
			if ( $line =~ /.*serial/ ) {
				my @lines = split(/\s+/, $line);
				
				if ($_[0] eq "name") {
					push(@ReturnArray, $lines[0]);
				} elsif ($_[0] eq "path") {
					push(@ReturnArray, $lines[1]);
				}
			}
		}
		
		close(FH);
		if ($_[1] eq "1") {
			@ReturnArray = sort(@ReturnArray);
		}
	}
	
	return \@ReturnArray;
}

# send_serialport(port_path, databits, baudrate, parity, stopbits, &position_array)
# send data to serialport
# example ("/dev/ttyUSB0", 8, 9600, "none", 1, \@array)
# 不要
sub send_serialport() {
	unless (eval{require Device::SerialPort}) {
		&error("Not Found Device::SerialPort");
		return;
	}

	my $port_path = $_[0];
	my $databits = $_[1];
	my $baudrate = $_[2];
	my $parity = $_[3];
	my $stopbits = $_[4];
	my $position_array_ref = $_[5];
	
	my $tty = Device::SerialPort -> new($port_path);
	
	$tty -> databits($databits);
	$tty -> baudrate($baudrate);
	$tty -> parity($parity);
	$tty -> stopbits($stopbits);
	$tty -> lookclear();
	
	select(undef, undef, undef, 5);
	
	foreach my $value (@$position_array_ref) {
		$tty -> write($value);
		select(undef, undef, undef, 0.02);
	}
	
	$tty -> close;
	return;
}

# convert_image(input_filepath, output_filepath)
# image format convert
sub convert_image() {
	unless (eval{require Image::Magick}) {
		&error("Not Found Image::Magick");
		return;
	}
	
	if (@_ != 0 && -e $_[0]) {
		my $image = Image::Magick -> new;
		$image -> Read($_[0]);
		$image -> Write($_[1]);
	} else {
		&error("Not Found Image file!");
	}
	
	return;
}

# image2string(ppm_filepath)
# image binary to numeric value
# array[0] is image width, array[1] is image height, others is color value
sub image2string() {
	my $filepath = $_[0];
	my @ReturnArray = ();
	my $line;
	
	open (IMAGE, "<$filepath");
	binmode (IMAGE);
	
	$line = <IMAGE>;  # Header Code
	$line = <IMAGE>;  # Comment or Size
	
	if ($line =~ /^#.*/) {
		$line = <IMAGE>;  # Size
	}
	
	my @size = split(/\s+/, $line);
	push(@ReturnArray, $size[0]);
	push(@ReturnArray, $size[1]);
	
	$line = <IMAGE>;  # Brightness
	
	# ppm (full color image) format
	if ($filepath =~ m/\.ppm$/) {
		while (read(IMAGE, $buf, 1)) {
			($red, $green, $blue) = unpack("CCC", $buf);
			push(@ReturnArray, $red);
			push(@ReturnArray, $green);
			push(@ReturnArray, $blue);
		}
	}
	
	# pgm (gray scale image) format
	if ($filepath =~ m/\.pgm$/) {
		while (read(IMAGE, $buf, 1)) {
			$brightness = unpack("C", $buf);
			push(@ReturnArray, $brightness);
		}
	}
	
	return \@ReturnArray;
}

# serial2time(&serial, [type])
# type = 1 then show locale language
# serial to time string
sub serial2time() {
	my $serial = $_[0];
	my $type = $_[1];
	
	my ($sec, $min, $hour, $mday, $mon, $year) = (localtime(substr($_[0], 0, 10)))[0,1,2,3,4,5];
	$sec = sprintf("%.2d", $sec);
	$min = sprintf("%.2d", $min);
	$hour = sprintf("%.2d", $hour);
	$mday = sprintf("%.2d", $mday + 1);
	$mon = sprintf("%.2d", $mon);
	$year += 1900;
	
	if ($type eq "1") {
		return "$year年$mon月$mday日$hour時$min分$sec秒";
	} else {
		return "$year-$mon-$mday $hour:$min:$sec";
	}
}

# search_from_file(file_path, &match_array)
# show date + filename string to filename
sub search_from_file() {
	my $file_path = $_[0];
	my $match_array_ref = $_[1];
	
	my @ReturnArray = ();
	
	my %filelist = ();
	&read_file($file_path, \%filelist);
	
	my @filelist_key = keys(%filelist);
	foreach my $key (@filelist_key) {
		my $serialdate = $key;
		$serialdate =~ s/\..*$//;
		$serialdate = substr($serialdate, 0, 10);
		
		foreach my $matching (@$match_array_ref) {
			my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($serialdate))[0,1,2,3,4,5];
			$sec = sprintf("%.2d", $sec);
			$min = sprintf("%.2d", $min);
			$hour = sprintf("%.2d", $hour);
			$mday = sprintf("%.2d", $mday + 1);
			$mon = sprintf("%.2d", $mon);
			$year += 1900;
			
			if ("$year-$mon-$mday $hour:$min:$sec $filelist{$key}" eq $matching) {
				push(@ReturnArray, $key);
			}
		}
	}
	
	return \@ReturnArray;
}

# swap_hash(&hash)
# swap key and value
sub swap_hash() {
	my $hash_ref = $_[0];
	my %ReturnHash = ();
	
	foreach my $key (keys(%$hash_ref)) {
		foreach my $value (values(%$hash_ref)) {
			$ReturnHash{$value} = $key;
		}
	}
	
	return \%ReturnHash;
}

# show_hash(&hash)
# show key and value
sub show_hash() {
	my $hash_ref = $_[0];
	my $cnt = 1;
	my $ReturnValue = "";
	
	while (($key, $value) = each(%{$hash_ref})) {
		$ReturnValue .= "$cnt. $key = $value<br>\n";
		$cnt++;
	}
	
	return $ReturnValue;
}

# show_pattern(X size, Y size, limmit0-255, &pattern, [reverse])
# show image pattern from string
# show key and value
sub show_pattern() {
	my $x_size = $_[0];
	my $y_size = $_[1];
	my $limmit = $_[2];
	my $pattern_ref = $_[3];
	my $reverse = $_[4];
	
	my $cnt = 1;
	
	foreach my $value (@$pattern_ref) {
		if ($reverse eq "0") {
			if ($value <= $limmit) {
				print "■ ";
			} else {
				print "　 ";
			}
		} else {
			if ($value <= $limmit) {
				print "　 ";
			} else {
				print "■ ";
			}
		}
		
		if ($cnt < $x_size) {
			$cnt++;
		} else {
			print "<br>";
			$cnt = 1;
		}
	}
}

